﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;


public enum VIPStringEnum {
    ALL_CHAR,
    LETTER_CHAR,
    NUMBER_CHAR
}


public class VIPRandomUtil {

    /// <summary>
    /// 枚举对应字典
    /// </summary>
    private static Dictionary<VIPStringEnum, string> strDic = new Dictionary<VIPStringEnum, string>();
    
    /// <summary>
    /// 向字典中添加键值对
    /// </summary>
    static VIPRandomUtil() {
        strDic.Add(VIPStringEnum.ALL_CHAR, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        strDic.Add(VIPStringEnum.LETTER_CHAR, "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        strDic.Add(VIPStringEnum.NUMBER_CHAR, "0123456789");
    }

    /// <summary>
    /// 返回一个定长的随机字符串
    /// </summary>
    /// <param name="length">长度</param>
    /// <returns>随机字符串</returns>
    public static string GetString(int length, VIPStringEnum stringEnum = VIPStringEnum.ALL_CHAR)
    {
        StringBuilder sb = new StringBuilder();
        System.Random random = new System.Random();
        string str = GetStrDic(stringEnum);
        for (int i = 0; i < length; i++)
        {
            sb.Append(str[(random.Next(0, str.Length))]);
        }
        return sb.ToString();
    }

    /// <summary>
    /// 生成UUID
    /// </summary>
    /// <param name="bo">是否返回纯数字+字符</param>
    /// <returns>UUID</returns>
    public static string GetUUID(bool bo = false) {
        string uuid = null;
        if (bo)
        {
            //返回纯数字+字符
            uuid = System.Guid.NewGuid().ToString("N");
        }
        else {
            //uuid字符串中带'-'
            uuid = System.Guid.NewGuid().ToString();
        }
        return uuid;
    }

    /// <summary>
    /// 随机m个m以内无重复的数字
    /// </summary>
    /// m : m以内无重复的数字
    /// <returns> 无重复的一维数组 </returns>
    public static int[] GetInts(int m)
    {
        int[] ran = new int[m];
        for (int i = 0; i < m;)
        {
            bool flag = false;
            int r = Random.Range(1, m + 1);
            for (int j = 0; j < m; j++)
            {
                if (ran[j] == r)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                ran[i] = r;
                i++;
            }
        }
        return ran;
    }

    /// <summary>
    /// 随机m个n以内无重复的数字
    /// </summary>
    /// n : n以内无重复的数字
    /// m : 需要获得n个数字
    /// <returns> 无重复的一维数组 </returns>
    public static int[] GetInts(int m, int n)
    {
        if (n < m) {
            return null;
        }
        int[] ran = new int[m];
        for (int i = 0; i < m;)
        {
            bool flag = false;
            int r = Random.Range(1, n + 1);
            for (int j = 0; j < m; j++)
            {
                if (ran[j] == r)
                {
                    flag = true;
                }
            }
            if (!flag)
            {
                ran[i] = r;
                i++;
            }
        }
        return ran;
    }

    /// <summary>
    /// 根据枚举在字典中查找对应的字符串
    /// </summary>
    /// <param name="stringEnum">字符串枚举</param>
    /// <returns>字典中枚举对应的字符串</returns>
    private static string GetStrDic(VIPStringEnum stringEnum) {
        string str = null;
        strDic.TryGetValue(stringEnum, out str);
        return str;
    }

}
